//
//  SearchViewController.m
//  GSG
//
//  Created by dllo on 15/4/25.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "SearchViewController.h"
#import"AFNetworking.h"
#import"AFNetworkingTool.h"
#import"SongModel.h"
#import"PlayerViewController.h"
#import"PlayListViewController.h"
#import"singleSong.h"
@interface SearchViewController ()<UISearchBarDelegate>
@property(nonatomic,retain)NSMutableArray *arrModel;//装搜索出来的歌曲的数组
@property(nonatomic,retain)NSMutableArray *arr;
@property(nonatomic,retain)UITableView*tableview;

@property(nonatomic,copy)NSString*str;
@property(nonatomic,retain)UISearchBar *bar;
@property(nonatomic,assign)BOOL result;
@end
@implementation SearchViewController

- (void)dealloc
{
    [_arrModel release];
    [_tableview release];
    [_bar release];
    [_arr release];
    [super dealloc];
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arrModel = [[NSMutableArray alloc]init];
        
        self.arr = [[NSMutableArray alloc]init];

    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"播放" style:UIBarButtonItemStyleDone target:self action:@selector(actionBuLand)];
    self.navigationItem.rightBarButtonItem.tintColor=[UIColor whiteColor];
    
    self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"bujing"]];
    
    self.bar=[[UISearchBar  alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height * 60/667)];

    self.bar.autocapitalizationType=UITextAutocapitalizationTypeWords;
    self.bar.autocorrectionType=UITextAutocorrectionTypeYes;
    self.bar.showsScopeBar = YES;
    self.bar.showsBookmarkButton = YES;
    self.bar.showsSearchResultsButton = YES;
    [self.bar setBackgroundImage:[UIImage imageNamed:@"bujing"]];
    self.bar.placeholder = @"搜索";
    
    self.bar.delegate=self;

    
    [self.view addSubview:self.bar];
    [_bar release];

    self.tableview= [[UITableView alloc]initWithFrame:CGRectMake(0, 120, self.view.frame.size.width *375 / 375, self.view.frame.size.height *667 / 667-self.view.frame.size.height * 170 / 667) style:UITableViewStylePlain];
    self.tableview.backgroundColor = [UIColor clearColor];
    self.tableview.dataSource=self;
    self.tableview.delegate=self;
    [self.view addSubview:self.tableview];
    [_tableview release];
    //点击空白处隐藏键盘
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];
    //设置成NO表示当前控件响应后会传播到其他控件上，默认为YES。
   tapGestureRecognizer.cancelsTouchesInView = NO;
    //将触摸事件添加到当前view
    [self.view addGestureRecognizer:tapGestureRecognizer];
 
}
//隐藏键盘
-(void)keyboardHide:(UITapGestureRecognizer*)tap{
    [self.bar resignFirstResponder];
}
-(void)data{
 
        if (![self.bar.text isEqualToString:@""]) {
        
            NSString *strURLEncode = [self.bar.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            
        NSString *str=[NSString stringWithFormat:@"http://so.ard.iyyin.com/s/song_with_out?q=%@&page=1&size=50&app=ttpod&v=v7.7.0.2015012818&uid=&mid=iPhone4&f=f320&s=s310&imsi=&hid=&splus=7.1.1&active=1&net=2&openudid=ad6f50f586818c4f36ff4358053c1b53fee58cdf&idfa=E2EAC64B-1F1C-4AC5-A0E1-8BE37A03FB96&utdid=VTCyBQSkYzwDAALO9zZTFfPC&alf=201200",strURLEncode];
        [AFNetworkingTool NetWorkingTooStr:str block:^(id result) {
            
            NSMutableDictionary *dic=result;
            self.arr=[dic objectForKey:@"data"];
            
      if(self.arr.count!=0){
                for (NSInteger i = 0; i < self.arr.count ; i++) {
                  SongModel *mo = [[SongModel alloc]init];
                    NSMutableDictionary *dic1 = self.arr[i];
                    mo.song_name=[dic1 objectForKey:@"song_name"];
                    mo.singer_name=[dic1 objectForKey:@"singer_name"];
                    NSMutableArray *arr = [dic1 objectForKey:@"url_list"];
                    
                    for (NSDictionary *dic  in arr) {
                        mo.url=[dic objectForKey:@"url"];
                        
                    }
                    [self.arrModel addObject:mo];
                    [mo release];
                }
            [self.tableview reloadData];
           }
        }];
        
    }

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(self.arrModel.count==0){
        return 0;
    }
    else{
    return self.arrModel.count-1  > 0? self.arrModel.count: 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *TableSampleIdentifier = @"TableSampleIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                             TableSampleIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:TableSampleIdentifier];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    if(self.arr.count!=0){
    NSString *str = [NSString stringWithFormat:@"%ld.%@",indexPath.row+1,self.arr[indexPath.row][@"song_name"]];
    cell.textLabel.text = str;
    }
   
    return cell;
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;
{
    [self.arrModel removeAllObjects];//将上一次搜索的数组清空

    [self data];
}


//跳转
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    
//    PlayerViewController *pvc=[[PlayerViewController alloc]init];
//    [self.navigationController pushViewController:pvc animated:YES];
//    
//    pvc.num=indexPath.row;
//    pvc.arr=self.arrModel;
//    [pvc release];
    [singleSong shareSingleSong].arrSong=self.arrModel;
    [singleSong shareSingleSong].num=indexPath.row;
    
    NSNotificationCenter *center=[NSNotificationCenter  defaultCenter];
    
    [center postNotificationName:@"播放" object:@"收到了看见了" userInfo:nil];
}
-(void)actionBuLand{
    PlayListViewController *plvc=[[PlayListViewController alloc]init];
    [self.navigationController pushViewController:plvc animated:YES];
    [plvc release];
    
}


- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
